package cn.cj.erp.modules.system.controller;

import cn.cj.erp.bo.system.SysLogBo;
import cn.cj.erp.common.enums.LogBusinessTypeEnum;
import cn.cj.erp.common.Page;
import cn.cj.erp.common.excel.util.ExcelUtil;
import cn.cj.erp.common.http.Result;
import cn.cj.erp.common.security.annotation.Log;
import cn.cj.erp.common.utils.ServletUtils;
import cn.cj.erp.modules.common.controller.AbstractController;
import cn.cj.erp.service.system.SysLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 系统日志
 *
 * @author chenjian
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/system/log")
public class SysLogController extends AbstractController {
    @Resource
    private SysLogService sysLogService;

    /**
     * 列表
     */
    @PostMapping("/list")
    @PreAuthorize("@ss.hasPerm('system:log:list')")
    public Result<Page<SysLogBo>> list(@RequestBody SysLogBo params) {
        return Result.query(sysLogService.queryPage(params));
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@ss.hasPerm('system:log:query')")
    public Result<SysLogBo> info(@PathVariable("id") Long id) {
        return Result.query(sysLogService.getById(id));
    }

    /**
     * 导出
     */
    @Log(title = "操作日志", businessType = LogBusinessTypeEnum.EXPORT, excludeParamNames = {"params"})
    @PreAuthorize("@ss.hasPerm('system:log:export')")
    @PostMapping("/export")
    public void export(@RequestBody SysLogBo params) {
        List<SysLogBo> list = sysLogService.queryList(params);
        ExcelUtil.exportExcel(SysLogBo.class, list, ServletUtils.getResponse(), "操作日志");
    }

    /**
     * 删除操作日志
     */
    @PostMapping("/delete/{ids}")
    @PreAuthorize("@ss.hasPerm('system:log:remove')")
    @Log(title = "操作日志", businessType = LogBusinessTypeEnum.DELETE)
    public Result<Boolean> delete(@PathVariable("ids") Long[] ids) {
        return Result.remove(sysLogService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 清空操作日志
     */
    @PostMapping("/clean")
    @PreAuthorize("@ss.hasPerm('system:log:remove')")
    @Log(title = "操作日志", businessType = LogBusinessTypeEnum.CLEAN)
    public Result<Boolean> clean() {
        return Result.ok(sysLogService.clean());
    }
}
